import { _decorator, Component, HingeJoint2D, Node, screen, UITransform, v3, Vec3, view } from 'cc';
import { DataMgr } from '../manager/DataMgr';
import { Ins } from '../Ins';
import { PoolMgr } from '../manager/PoolMgr';
const { ccclass, property } = _decorator;

@ccclass('Block')
export class Block extends Component {

    update(deltaTime: number) {
        const { width, height } = this.node.getComponent(UITransform)
        const { y } = this.node.getPosition()
        const maxSize = Math.max(width, height)
        const maxHeight = view.getVisibleSize().height
        const outY = -((maxHeight + maxSize) / 2)
        if (y <= outY) {
            this.node.removeFromParent()
            DataMgr.ins.cleared += 1
            Ins.GameMgr.onGameCheck()
        }
    }

    getCorePosArr() {
        const joints = this.getComponents(HingeJoint2D)
        const corePosArr: Vec3[] = []
        for (let i = 0; i < joints.length; i++) {
            const joint = joints[i]
            const anchor = joint.anchor
            const core = PoolMgr.ins.getNode('Core', this.node, v3(anchor.x, anchor.y, 0))
            const wpos = core.getComponent(UITransform).convertToWorldSpaceAR(v3(0, 0, 0))
            corePosArr.push(v3(wpos.x, wpos.y, wpos.z))
        }
        return corePosArr
    }
}


